package genericdemo;

/**
 * @Author 12629
 * @Description：
 */
//写一个泛型类，找出数组当中的最大值,只要这个T，实现了这个接口就行
class Alg<T extends Comparable<T>> {
    public T findMaxVal(T[] array) {
        T maxVal = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(maxVal) > 0) {
                maxVal = array[i];
            }
        }
        return maxVal;
    }
}
class Alg2 {
    //静态方法
    public static<T extends Comparable<T>> T findMaxVal(T[] array) {
        T maxVal = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(maxVal) > 0) {
                maxVal = array[i];
            }
        }
        return maxVal;
    }
}

class Alg3 {
    //泛型方法:成员方法
    public <T extends Comparable<T> > T findMaxVal2(T[] array) {
        T maxVal = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(maxVal) > 0) {
                maxVal = array[i];
            }
        }
        return maxVal;
    }
}
public class TestDemo2 {

    public static void main(String[] args) {
        Alg3 alg3 = new Alg3();
        Integer[] array = {78,23,190,2,9,10};
        int  val = alg3.findMaxVal2(array);
        System.out.println(val);
    }

    public static void main2(String[] args) {
        Integer[] array = {78,23,190,2,9,10};
        //int val = genericdemo.Alg2.<Integer>findMaxVal(array);
        int val = Alg2.findMaxVal(array);
        System.out.println(val);
    }


    public static void main1(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] array = {78,23,190,2,9,10};
        int val = alg.findMaxVal(array);
        System.out.println(val);
    }
}
